package com.jie.server.mvc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jie.common.http.constant.HttpEntityClassNameConstant;
import com.jie.common.http.constant.HttpHeaderLineNameConstant;
import com.jie.common.http.constant.response.ServerResponseStatusConstant;
import com.jie.common.http.entity.request.HttpRequest;
import com.jie.common.http.entity.response.HttpResponse;
import com.jie.common.util.JsonUtil;
import com.jie.server.core.annotation.Mapping;
import com.jie.server.mvc.model.entity.Follow;
import com.jie.server.mvc.model.entity.User;
import com.jie.server.mvc.model.vo.FollowQueryVo;
import com.jie.server.mvc.model.vo.FollowUserVo;
import com.jie.server.mvc.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 关注表 前端控制器
 * </p>
 *
 * @author XieZhiJie
 * @since 2022-04-24
 */
@Controller("follow")
public class FollowController {

    private final FollowService followService;

    @Autowired
    public FollowController(
            FollowService followService
    ) {
        this.followService = followService;
    }

    /**
     * 添加一条关注信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse add(HttpRequest httpRequest, HttpResponse httpResponse) {
        Follow follow = JsonUtil.fromJson(httpRequest.getEntityString(), Follow.class);
        follow.setId(null);
        follow.setUserId(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.followService.add(follow)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.FOLLOW_ADD_SUCCESS);
            httpResponse.setEntity("添加关注成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.FOLLOW_ADD_FAIL);
            httpResponse.setEntity("添加关注失败! 用户不存在或已关注!");
        }
        return httpResponse;
    }

    /**
     * 分页获取关注的用户信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getFollowUserList(HttpRequest httpRequest, HttpResponse httpResponse) {
        FollowQueryVo followQueryVo = JsonUtil.fromJson(httpRequest.getEntityString(), FollowQueryVo.class);
        Map<String, Object> map = this.followService.getFollowUserList(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)), followQueryVo);
        httpResponse.addOneHeaderLine("total", String.valueOf(map.get("total")));
        httpResponse.setEntity(JsonUtil.toJson(map.get("records")));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_FOLLOW_USER_LIST_SUCCESS);
        return httpResponse;
    }

    /**
     * 获取在线的关注的用户信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getOnlineFollowUserList(HttpRequest httpRequest, HttpResponse httpResponse) {
        Integer userId = Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE));
        List<FollowUserVo> followUserVoList = this.followService.getOnlineFollowUserList(userId);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_ONLINE_FOLLOW_USER_LIST_SUCCESS);
        httpResponse.setEntity(JsonUtil.toJson(followUserVoList));
        return httpResponse;
    }

    /**
     * 删除一个关注
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse cancel(HttpRequest httpRequest, HttpResponse httpResponse) {
        Integer userId = Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE));
        Integer otherUserId = Integer.parseInt(httpRequest.getEntityString());
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.eq("user_id", userId);
        followQueryWrapper.eq("other_user_id", otherUserId);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.followService.remove(followQueryWrapper)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CANCEL_FOLLOW_SUCCESS);
            httpResponse.setEntity("删除关注成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CANCEL_FOLLOW_FAIL);
            httpResponse.setEntity("删除关注失败!");
        }
        return httpResponse;
    }

    /**
     * 更新一个关注
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse update(HttpRequest httpRequest, HttpResponse httpResponse) {
        Follow follow = JsonUtil.fromJson(httpRequest.getEntityString(), Follow.class);
        follow.setId(null);
        follow.setUserId(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.followService.update(follow)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.UPDATE_FOLLOW_SUCCESS);
            httpResponse.setEntity("修改关注成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.UPDATE_FOLLOW_FAIL);
            httpResponse.setEntity("修改关注失败!");
        }
        return httpResponse;
    }

    /**
     * 获取未关注用户列表
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getNotFollowUserList(HttpRequest httpRequest, HttpResponse httpResponse) {
        Integer userId = Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_NOT_FOLLOW_USER_LIST_SUCCESS);
        List<User> userList = this.followService.getNotFollowUserList(userId);
        if (userList == null) {
            userList = new LinkedList<>();
        }
        httpResponse.setEntity(JsonUtil.toJson(userList));
        return httpResponse;
    }

}
